/*框架权限控制操作类
Created on 2020年05月10日
@author: zxyong 13738196011
 */

package com.zxy.business;

import java.text.SimpleDateFormat;
import java.util.Date;

import com.zxy.model.*;

//接口权限控制
public class Power_Control
{
	private Ope_DB_Cent		opd				= new Ope_DB_Cent();
	private T_SUB_SYS		tss				= null;
	private T_PROC_NAME		tpn				= null;
	private T_SUB_POWER		tsp				= null;
	private T_SUB_USER		tsu				= null;
	private T_SUB_USERPOWER	tsup			= null;
	private String			sub_code		= "";
	private String			sub_usercode	= "";
	private String			param_name		= "";
	private StringBuilder	error			= new StringBuilder();
	private String			SUB_IP			= "";

	public Power_Control(String ssub_code,String ssub_usercode,String param_name,String sub_ip)
	{
		this.setSub_code(ssub_code);
		this.setSub_usercode(ssub_usercode);
		this.setParam_name(param_name);
		this.setSub_Ip(sub_ip);
		this.setTss(opd.Get_T_SUB_SYS(sub_code));
		this.setTpn(opd.Get_T_PROC_NAME(param_name,tss.MAIN_ID));
		this.setTsp(opd.Get_T_SUB_POWER(tss.MAIN_ID,tpn.MAIN_ID));
		this.setTsu(opd.Get_T_SUB_USER(tss.MAIN_ID,ssub_usercode));
		this.setTsup(opd.Get_T_SUB_USERPOWER(tsu.MAIN_ID,tpn.MAIN_ID));
		this.setError(Ope_Power());
	}

	private StringBuilder Ope_Power()
	{
		StringBuilder strResult = new StringBuilder();
		if(this.tss.MAIN_ID == -1)
			strResult.append("{\"" + param_name + "\":[{\"s_result\":\"0\",\"error_desc\":\"该业务平台code未授权\"}]}");
		else if(this.tpn.MAIN_ID == -1)
			strResult.append("{\"" + param_name + "\":[{\"s_result\":\"0\",\"error_desc\":\"该接口未定义，或未关联平台与接口权限\"}]}");
		else if(new Date().compareTo(this.getTss().LIMIT_DATE) == 1)
			strResult.append("{\"" + param_name + "\":[{\"s_result\":\"0\",\"error_desc\":\"该业务平台超过使用期限,期限时间为" + (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(this.getTss().LIMIT_DATE)) + "\"}]}");
		else if(this.tsp.MAIN_ID != -1 && new Date().compareTo(this.tsp.LIMIT_DATE) == 1)
			strResult.append("{\"" + param_name + "\":[{\"s_result\":\"0\",\"error_desc\":\"该业务平台对接口调用超过使用期限,期限时间为:" + (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(this.tsp.LIMIT_DATE)) + "\"}]}");
		else if(this.tsu.MAIN_ID != -1 && new Date().compareTo(this.tsu.LIMIT_DATE) == 1)
			strResult.append("{\"" + param_name + "\":[{\"s_result\":\"0\",\"error_desc\":\"该用户超过使用期限,期限时间为:" + (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(this.tsu.LIMIT_DATE)) + "\"}]}");
		else if(this.tsup.MAIN_ID != -1 && new Date().compareTo(this.tsup.LIMIT_DATE) == 1)
			strResult.append("{\"" + param_name + "\":[{\"s_result\":\"0\",\"error_desc\":\"该用户对接口调用超过使用期限,期限时间为:" + (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(this.tsup.LIMIT_DATE)) + "\"}]}");
		else if(!this.tss.IP_ADDR.equals("") && !this.tss.IP_ADDR.equals("0.0.0.0") && this.tss.IP_ADDR.indexOf("|" + this.SUB_IP + "|") == -1)
			strResult.append("{\"" + param_name + "\":[{\"s_result\":\"0\",\"error_desc\":\"该业务平台调用IP未授权,您的IP:" + this.SUB_IP + "\"}]}");
		else if(this.tsp.MAIN_ID != -1 && this.tsp.USE_LIMIT > this.tsp.LIMIT_NUMBER)
		{
			Date dtNow = new Date();
			strResult.append("{\"" + param_name + "\":[{\"s_result\":\"0\",\"error_desc\":\"该业务平台超过调用次数,次数限制为" + this.tsp.LIMIT_NUMBER + "次");
			// 1:年为基础 2:月为基础 3:日为基础
			if(this.tsp.LIMIT_TAG.compareTo("1") == 0 && new SimpleDateFormat("yyyy").format(dtNow).compareTo(new SimpleDateFormat("yyyy").format(this.tsp.FIRST_DATE)) == 0)
				strResult.append("每年,本年已调用" + this.tsp.USE_LIMIT + "次\"}]}");
			else if(this.tsp.LIMIT_TAG.compareTo("2") == 0 && new SimpleDateFormat("yyyy-MM").format(dtNow).compareTo(new SimpleDateFormat("yyyy-MM").format(this.tsp.FIRST_DATE)) == 0)
				strResult.append("每月,本月已调用" + this.tsp.USE_LIMIT + "次\"}]}");
			else if(this.tsp.LIMIT_TAG.compareTo("3") == 0 && new SimpleDateFormat("yyyy-MM-dd").format(dtNow).compareTo(new SimpleDateFormat("yyyy-MM-dd").format(this.tsp.FIRST_DATE)) == 0)
				strResult.append("每日,本日已调用" + this.tsp.USE_LIMIT + "次\"}]}");
			else strResult.append("\"}]}");
		}
		else if(this.tsup.MAIN_ID != -1 && this.tsup.USE_LIMIT > this.tsup.LIMIT_NUMBER)
		{
			Date dtNow = new Date();
			strResult.append("{\"" + param_name + "\":[{\"s_result\":\"0\",\"error_desc\":\"该用户对接口调用超过调用次数,次数限制为" + this.tsup.LIMIT_NUMBER + "次");
			// 1:年为基础 2:月为基础 3:日为基础
			if(this.tsup.LIMIT_TAG.compareTo("1") == 0 && new SimpleDateFormat("yyyy").format(dtNow).compareTo(new SimpleDateFormat("yyyy").format(this.tsup.FIRST_DATE)) == 0)
				strResult.append("每年,本年已调用" + this.tsup.USE_LIMIT + "次\"}]}");
			else if(this.tsup.LIMIT_TAG.compareTo("2") == 0 && new SimpleDateFormat("yyyy-MM").format(dtNow).compareTo(new SimpleDateFormat("yyyy-MM").format(this.tsup.FIRST_DATE)) == 0)
				strResult.append("每月,本月已调用" + this.tsup.USE_LIMIT + "次\"}]}");
			else if(this.tsup.LIMIT_TAG.compareTo("3") == 0 && new SimpleDateFormat("yyyy-MM-dd").format(dtNow).compareTo(new SimpleDateFormat("yyyy-MM-dd").format(this.tsup.FIRST_DATE)) == 0)
				strResult.append("每日,本日已调用" + this.tsup.USE_LIMIT + "次\"}]}");
			else strResult.append("\"}]}");
		}
		return strResult;
	}

	public T_SUB_SYS getTss()
	{
		return tss;
	}

	public void setTss(T_SUB_SYS tss)
	{
		this.tss = tss;
	}

	public T_SUB_POWER getTsp()
	{
		return tsp;
	}

	public void setTsp(T_SUB_POWER tsp)
	{
		this.tsp = tsp;
	}

	public T_SUB_USER getTsu()
	{
		return tsu;
	}

	public void setTsu(T_SUB_USER tsu)
	{
		this.tsu = tsu;
	}

	public T_SUB_USERPOWER getTsup()
	{
		return tsup;
	}

	public void setTsup(T_SUB_USERPOWER tsup)
	{
		this.tsup = tsup;
	}

	public T_PROC_NAME getTpn()
	{
		return tpn;
	}

	public void setTpn(T_PROC_NAME tpn)
	{
		this.tpn = tpn;
	}

	public String getSub_code()
	{
		return sub_code;
	}

	public void setSub_code(String sub_code)
	{
		this.sub_code = sub_code;
	}

	public String getSub_usercode()
	{
		return sub_usercode;
	}

	public void setSub_usercode(String sub_usercode)
	{
		this.sub_usercode = sub_usercode;
	}

	public String getSub_Ip()
	{
		return SUB_IP;
	}

	public void setSub_Ip(String sub_ip)
	{
		this.SUB_IP = sub_ip;
	}

	public String getParam_name()
	{
		return param_name;
	}

	public void setParam_name(String param_name)
	{
		this.param_name = param_name;
	}

	public StringBuilder getError()
	{
		return error;
	}

	public void setError(StringBuilder error)
	{
		this.error = error;
	}
}